
#include "SysUtil.h"
#include <atlbase.h>

CSysUtil::CSysUtil(void)
{
}

CSysUtil::~CSysUtil(void)
{
}

BOOL CSysUtil::mkdir(const CString &path)
{
	if(CreateDirectory(path, NULL))
		return TRUE;
	if(GetLastError() == ERROR_ALREADY_EXISTS)
		return TRUE;
	return FALSE;
}

BOOL CSysUtil::SetRegValue(HKEY h, const CString &key,
						   const CString &name, const CString &value)
{
	CRegKey regkey;
	LONG lRet = regkey.Open(h, key);
	if( lRet != ERROR_SUCCESS )
	{
		if (lRet == ERROR_FILE_NOT_FOUND)
		{
			lRet = regkey.Create(h, key);
			if( lRet != ERROR_SUCCESS)
			{
				return FALSE;
			}
		}
	}

	lRet = regkey.SetStringValue(name, value);
	if( lRet != ERROR_SUCCESS )
	{
		regkey.Close();
		return FALSE;
	}

	regkey.Flush();
	regkey.Close();
	return TRUE;
}

BOOL CSysUtil::GetRegValue(HKEY h, const CString &key,
						   const CString &name, CString &value)
{
	CRegKey regkey;
	LONG lRet = regkey.Open(h, key);
	if( lRet != ERROR_SUCCESS )
	{
		return FALSE;
	}

	char buffer[1024];
	ULONG size = sizeof(buffer);
	lRet = regkey.QueryStringValue(name, buffer, &size);
	if( lRet != ERROR_SUCCESS )
	{
		regkey.Close();
		return FALSE;
	}

	value.SetString(buffer, size);

	regkey.Close();
	return TRUE;
}

BOOL CSysUtil::HideFile(const CString &path)
{
#ifdef _DEBUG
	return SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN );
#else
	return SetFileAttributes(path, FILE_ATTRIBUTE_HIDDEN | FILE_ATTRIBUTE_SYSTEM);
#endif
}

CString CSysUtil::GetHostName()
{
	char buffer[128] = { 0 };
	if( 0 == ::gethostname(buffer, sizeof(buffer)))
		return buffer;
	else
		return "";
}